const {
    startApp,
    advclick
} = require("./Common");

auto.waitFor();
startApp("com.gxb.wallet.app");
console.show();
sleep(3000);
if (id("area").exists()) {
    id("area")
        .findOne(2000)
        .children()
        .forEach(function(gxc) {
            log("找到一个");
            gxc.click();
        });
    sleep(2000);
}
if (!text("开心夺宝").exists()) {
    log("点击展开");
    id("bt_dig_mine").findOne().click();
    sleep(1500);
}
log("点击夺宝");
advclick(text("开心夺宝").findOne(1000));
sleep(1500);
getmoney();
let count = text("换一批").findOne().parent().children().get(1);
while (count.text() !== "今日名单剩余0") {
    log(count.text());
    //判断是否获取完毕
    let t = textStartsWith("算力")
        .find()
        .filter((item) => {
            if (item.parent().childCount() > 4) {
                log(item.parent().child(3).child(0).text());
                return item.parent().child(3).child(0).text() === "";
            } else {
                return false;
            }
        });
    if (t.length === 0) {
        text("换一批").findOne().parent().click();
    } else {
        log("还没收取完毕继续收取");
    }
    sleep(2000);
    getmoney();
    log("换一批");
    count = text("换一批").findOne().parent().children().get(1);
}
gesture(100, [0, device.height / 2], [100, device.height / 2]);
getPrice();
sendMessage();
log("结束");
console.hide();
exit();

function getmoney() {
    let sl = textStartsWith("算力").findOne(3000);
    if (sl) {
        advclick(sl);
        sleep(2000);
        while (text("切换下一位").exists()) {
            sleep(1500);
            const ca = currentActivity();
            const ad = (ca === "com.qq.e.ads.RewardvideoPortraitADActivity");
            if (ad) {
                log("观看视频即可领取");
                //ad.click();
                watchtv();
                sleep(500);
            }
            
            let n = 0;
            while(!id('area').exists()){
                n++;
                log('重试', n);
                if(n>20){
                    watchtv();
                    log('结束重试')
                }
                sleep(1000);
            }
            
            id("area")
                .findOne()
                .children()
                .forEach(function(item) {
                    item.click();
                });
                
            log("收取成功");
            sleep(2500);
            if (text("切换下一位").exists()) {
                advclick(text("切换下一位").findOne());
            }
        }
        gesture(100, [0, device.height / 2], [100, device.height / 2]);
        sleep(1500);
    } else {
        log("超时");
    }
}

function getPrice() {
    log("开始浏览实盘");
    sleep(1500);
    id("tv_pan").findOne().click();
    sleep(1500);
    log("开始浏览行情");
    id("tv_price").findOne().click();
    sleep(10000);
    const ok = id("bt_sure").findOne(3000);
    if (ok) {
        ok.click();
    }
    log("回到社区");
    id("tv_community").findOne().click();
}

function getNews() {
    var r = http.post(
        "https://webapi.8btc.com/bbt_api/news/list?post_type=post&num=1&page=page", {
            "USER-AGENT": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.6824.400 QQBrowser/10.3.3137.400",
        }
    );
    log("code = " + r.statusCode);
    const t = r.body.json();
    const list = t["data"]["list"];
    if (list.length > 0) {
        return list[0].desc
    } else {
        return '今天GXC咋样？'
    }
}

function sendMessage() {
    log("开始发消息");
    waitForActivity("com.gxb.ui.activity.HomeActivity");
    while (true) {
        log("手势");
        sleep(1000);
        if (id("fab").exists()) {
            break;
        }
        log(device.width, device.height);
        gesture(
            500, [device.width / 2, device.height * 0.8], [device.width / 2, device.height * 0.4]
        );
    }
    id("fab").findOne().click();
    sleep(1000);
    id("tv_publish_short").findOne().click();
    sleep(1000);
    const text = getNews();
    setClip(text);
    id("editor").findOne().paste();
    sleep(1000);
    id("bt_publish").findOne().click();
    log("结束发表");
}

function watchtv() {
    log("点击关闭");
    text("恭喜获得奖励").waitFor();
    className("android.widget.ImageView")
        .clickable(true)
        .find()
        .forEach((item, i) => {
            item.click();
            log("hh", i);
        });
    sleep(200);
    if (currentActivity() === "com.qq.e.ads.RewardvideoPortraitADActivity") {
        if (className("android.widget.ImageView").clickable(true).exists()) {
            className("android.widget.ImageView")
                .clickable(true)
                .findOne(1000)
                .click();
        }
    }
    sleep(200);
    if (className("android.widget.TextView").text("放弃奖励").exists()) {
        className("android.widget.TextView").text("放弃奖励").findOne().click();
    }
}